﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Ctrip.Demo.Utils
{
    public class HttpUtil
    {
        public static string Get(string url, object quaryStrings, string userAgent = "")
        {
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
#if DEBUG
            webRequest.Proxy = new WebProxy("10.14.7.11", 80);
#else
                webRequest.Proxy = new WebProxy("10.14.7.11", 80);
#endif
            webRequest.Method = "GET";
            webRequest.UserAgent = userAgent;

            IDictionary<string, object> paramsDict = quaryStrings.CreateValueDictionary();

            string quaryStr = string.Join("&", paramsDict.Select(kv => string.Format("{0}={1}", kv.Key, kv.Value)));
            if (url.IndexOf("?") >= 0)
            {
                url = string.Format("{0}&{1}", url, quaryStr);
            }
            else
            {
                url = string.Format("{0}?{1}", url, quaryStr);
            }


            string responseString = null;

            using (HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse)
            {
                using (Stream responseStream = webResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        responseString = streamReader.ReadToEnd();
                    }
                }
            }

            return responseString;
        }
        public static string Post(string url, object postData, string userAgent = "", string contentType = "application/x-www-form-urlencoded")
        {
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
#if DEBUG
            webRequest.Proxy = new WebProxy("10.14.7.11", 80);
#else
                webRequest.Proxy = new WebProxy("10.14.7.11", 80);
#endif
            webRequest.Method = "POST";
            webRequest.UserAgent = userAgent;
            webRequest.ContentType = contentType;

            IDictionary<string, object> dataDict = postData.CreateValueDictionary();

            string body = string.Join("&", dataDict.Select(kv => string.Format("{0}={1}", kv.Key, kv.Value)));

            byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
            using (MemoryStream postStream = new MemoryStream(bodyBytes))
            {
                postStream.Position = 0;
                webRequest.ContentLength = postStream.Length;
                using (Stream stream = webRequest.GetRequestStream())
                {
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                }
            }

            string responseString = null;

            using (HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse)
            {
                using (Stream responseStream = webResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        responseString = streamReader.ReadToEnd();
                    }
                }
            }

            return responseString;
        }
    }
}
